if(typeof com         == 'undefined') com         = {};
if(typeof com.lolita  == 'undefined') com.lolita  = {};
if(typeof com.lolita.display == 'undefined') com.lolita.display = {};

if (typeof com.lolita.display.antiPirate == 'undefined') com.lolita.display.antiPirate = {};
com.lolita.display.antiPirate = function (){
  alert("Espèce de pirate!");
  window.location.replace('http://www.association-lolita.com');
}

if(typeof com.lolita.display.ajaxLoad == 'undefined') com.lolita.display.ajaxLoad = {};
com.lolita.display.ajaxLoad  = function (container,params,callback){
  jQuery(container).load(com.lolita.get.ajaxUrl(),params,callback);
}

if(typeof com.lolita.display.get == 'undefined') com.lolita.display.get = {};

if(typeof com.lolita.display.get.loaderTemplate == 'undefined') com.lolita.display.get.loaderTemplate = {};
com.lolita.display.get.loaderTemplate  = function (pars){

  var p,def = {
    "imgPath" : './css/themes/'+com.lolita.get.themeName()+'/images/' 
    ,"img"    : 'loading.gif'
    ,"text"   : 'Patience'
  }
  
  p = jQuery.extend({},def,pars);
  var tpl = [];
  tpl.push('<div>');
  if(p.img!='')   tpl.push('<img src="'+(p.imgPath+p.img)+'" />');
  if(p.text!='')  tpl.push('<span>'+p.text+'</span>');
  tpl.push('</div>');
  
  return jQuery(tpl.join(''));
}

if(typeof com.lolita.dialog == 'undefined') com.lolita.dialog = {};
if(typeof com.lolita.dialog.removeEvents == 'undefined') com.lolita.dialog.removeEvents = {};

com.lolita.dialog.removeEvents = function(what){
  var $ic = com.lolita.get.ic();
  jQuery.each(['open','close','ajaxLoaded','resizeStop'],function(idx,method){
    $ic.unbind('loli.'+what+'.'+method);
  });
}

if(typeof com.lolita.dialog.dialogIc == 'undefined') com.lolita.dialog.dialogIc = {};
com.lolita.dialog.dialogIc  = function (pars){

  var $ic = com.lolita.get.ic();
  try{
      $ic.dialog("close").dialog("destroy");
  } catch(e){}
  
  $ic.bind('dialogopen',function(event,ui){
    
    if($ic.prev('.ajax-loading').length==0){
      var $loader = com.lolita.display.get.loaderTemplate({"text":"Patience"});
      
      var $widget = $ic.dialog('widget');
      
      var $titre  = jQuery('.ui-dialog-titlebar',$widget);
      $loader.addClass('ajax-loading ui-corner-all ui-widget-header ui-state-default').hide();
      $loader.ajaxStart(function(un,deux,trois){
        jQuery(this).addClass('ui-state-active').find('span').text('Patience').end().fadeIn(500);
      }).ajaxStop(function(){
        jQuery(this).switchClass('ui-state-active','ui-state-default').find('span').text('Cool').end().delay(300).fadeOut(800);
      });
      
      $ic.before($loader);
      
      
    }
  }).dialog(pars);
}

if(typeof com.lolita.dialog.teamAdmin == 'undefined') com.lolita.dialog.teamAdmin = {};
com.lolita.dialog.teamAdmin = function(name){
  com.lolita.dialog.team(name, {
     "show_admin" : true
    ,"type"       : 'team_admin'
    ,"height"     : 600
  });
}

if(typeof com.lolita.dialog.team == 'undefined') com.lolita.dialog.team = {};
com.lolita.dialog.team  = function (name,pars){
  var $ic = com.lolita.get.ic();
  
  pars = jQuery.extend(true,{"type":"team"},pars);
    
  var p,d = {
     "show_admin"   : false
    ,"callBack"     : jQuery.noop
    ,"onOpen"       : function(){
      $ic.trigger('loli.'+p.type+'.open');  
    }
    ,"onClose"      : function(){
      $ic.trigger('loli.'+p.type+'.close');
      com.lolita.dialog.removeEvents(p.type);
    }
    ,"onAjaxLoaded" : function(){ 
      $ic.trigger('loli.'+p.type+'.ajaxLoaded');  
    }
    ,"onResizeStop" : function(){ 
      $ic.trigger('loli.'+p.type+'.resizeStop');  
    }
    ,'width'      : 640
    ,'height'     : 500
    ,'opened'    : {
       "width"    : 0
      ,"height"   : 0
      }
    ,'modal'      : true
    ,'resizable'  : false
    ,'title'      : 'Chargement en cours...'
    ,'buttons'    : {
      'Fermer' : function(){
        $ic.dialog("close");
      }
    }
  }

  p = jQuery.extend(true,{},d,pars);

  if(p.show_admin==true){
    p.type = 'team_admin';
  }


  var p_dialog = jQuery.extend(true,{},p,{
    'open'       : function(event){
      if(jQuery.isFunction(p.callBack)) p.callBack('open');
      var $this = jQuery(this);
      $ic.html(' ');
      com.lolita.display.addAjaxLoading(this);
      if(jQuery.isFunction(p.callBack)) p.callBack('ajaxLoad');
      com.lolita.display.ajaxLoad($ic,{
         'action'     : 'show'
        ,'what'       : 'equipe'
        ,'team_name'  : name
        ,'show_admin' : p.show_admin
        },function(){
          if(jQuery.isFunction(p.onAjaxLoaded)){
            p.onAjaxLoaded();
            if(p.opened){
              if(p.opened.width){
                $this.dialog('option','width',p.opened.width);
              }
              if(p.opened.height){
                $this.dialog('option','height',p.opened.height);
              }
            }
          }
          if(jQuery.isFunction(p.callBack)) p.callBack('ajaxLoadEnd');
      });

      if(jQuery.isFunction(p.onOpen)) p.onOpen();
      if(jQuery.isFunction(p.callBack)) p.callBack('openEnd');
    }
    ,'close'      : function(){
      if(jQuery.isFunction(p.callBack)) p.callBack('close');
      if(jQuery.isFunction(p.onClose)) p.onClose();
      if(jQuery.isFunction(p.callBack)) p.callBack('closeEnd');
    }

  });


  com.lolita.dialog.dialogIc(p_dialog);

}

if (typeof com.lolita.dialog.newsDelete == 'undefined') com.lolita.dialog.newsDelete = {};
com.lolita.dialog.newsDelete= function (id,callback){
  debugger;
  var url = com.lolita.get.ajaxUrl();
  var pars = {
    'news_id' : id
    ,'action' : 'delete'
    ,'what'   : 'news'
    ,'statusCode' : {
        403: function() {
          com.lolita.display.antiPirate();
        }
      }
  }
  
  if(confirm("Supprimer cette news?")==false){
    return;
  }
  
  jQuery.post(url,pars,function(data, textStatus, jqXHR){
    if(callback && jQuery.isFunction(callback)){
      debugger;
      callback.call(data, textStatus, jqXHR);
    }
  });
}

if(typeof com.lolita.dialog.news == 'undefined') com.lolita.dialog.news = {};
com.lolita.dialog.news  = function (id,pars){
  var $ic = com.lolita.get.ic();
  
  pars = jQuery.extend(true,{"type":"news"},pars);
  
  var p,d = {
     "show_admin"   : false
    ,"callBack"     : jQuery.noop
    ,"onOpen"       : function(){
      $ic.trigger('loli.'+p.type+'.open');  
    }
    ,"onClose"      : function(){
      $ic.trigger('loli.'+p.type+'.close');
      com.lolita.dialog.removeEvents(p.type);
    }
    ,"onAjaxLoaded" : function(){ 
      $ic.trigger('loli.'+p.type+'.ajaxLoaded');  
    }
    ,"onResizeStop" : function(){ 
      $ic.trigger('loli.'+p.type+'.resizeStop');  
    }
    ,'width'      : 640
    ,'height'     : 500
    ,'opened'    : {
       "width"    : 0
      ,"height"   : 0
      }
    ,'modal'      : true
    ,'resizable'  : false
    ,'title'      : 'Chargement en cours...'
    ,'buttons'    : {
      'Sauver' : function(e){
        $ic.trigger('save');
      }
      ,'Fermer' : function(e){
        $ic.trigger("close");
      }
    }
  }

  p = jQuery.extend(true,{},d,pars);

  var p_dialog = jQuery.extend(true,{},p,{
    'open'       : function(event){
      if(jQuery.isFunction(p.callBack)) p.callBack('open');
      var $this = jQuery(this);
      $ic.html(' ');
      com.lolita.display.addAjaxLoading(this);
      if(jQuery.isFunction(p.callBack)) p.callBack('ajaxLoad');
      com.lolita.display.ajaxLoad($ic,{
         'action'       : 'get'
        ,'what'         : 'news'
        ,'news_id'      : id
        },function(rep){
          if(jQuery.isFunction(p.onAjaxLoaded)){
            p.onAjaxLoaded();
            if(p.opened){
              if(p.opened.width){
                $this.dialog('option','width',p.opened.width);
              }
              if(p.opened.height){
                $this.dialog('option','height',p.opened.height);
              }
            }
          }
          if(jQuery.isFunction(p.callBack)) p.callBack('ajaxLoadEnd');
      });

      if(jQuery.isFunction(p.onOpen)) p.onOpen();
      if(jQuery.isFunction(p.callBack)) p.callBack('openEnd');
    }
    ,'close'      : function(){
      if(jQuery.isFunction(p.callBack)) p.callBack('close');
      if(jQuery.isFunction(p.onClose)) p.onClose();
      if(jQuery.isFunction(p.callBack)) p.callBack('closeEnd');
    }
    ,'resizeStop'      : function(){
      if(jQuery.isFunction(p.onClose)) p.onResizeStop();
    }
    

  });
  
  com.lolita.dialog.dialogIc(p_dialog);

}

if(typeof com.lolita.dialog.player == 'undefined') com.lolita.dialog.player = {};
com.lolita.dialog.player  = function (name,pars){
  var $ic = com.lolita.get.ic();
  
  pars = jQuery.extend(true,{"type":"player"},pars);
  
  var p,d = {
     "show_admin"   : false
    ,"callBack"     : jQuery.noop
    ,"onOpen"       : function(){ 
      $ic.trigger('loli.'+p.type+'.open');  
    }
    ,"onClose"      : function(){
      $ic.trigger('loli.'+p.type+'.close');
      com.lolita.dialog.removeEvents(p.type);
    }
    ,"onAjaxLoaded" : function(){ 
      $ic.trigger('loli.'+p.type+'.ajaxLoaded');  
    }
    ,"onResizeStop" : function(){ 
      $ic.trigger('loli.'+p.type+'.resizeStop');  
    }
    ,'width'      : 640
    ,'height'     : 500
    ,'opened'    : {
       "width"    : 0
      ,"height"   : 0
      }
    ,'modal'      : true
    ,'resizable'  : false
    ,'title'      : 'Chargement en cours...'
    ,'buttons'    : {
      'Fermer' : function(){
        $ic.dialog("close");
      }
    }
  }

  p = jQuery.extend(true,{},d,pars);
  
  if(p.show_admin==true){
    p.type = 'player_admin';
  }

  var p_dialog = jQuery.extend(true,{},p,{
    'open'       : function(event){
      if(jQuery.isFunction(p.callBack)) p.callBack('open');
      var $this = jQuery(this);
      $ic.html(' ');
      com.lolita.display.addAjaxLoading(this);
      if(jQuery.isFunction(p.callBack)) p.callBack('ajaxLoad');
      com.lolita.display.ajaxLoad($ic,{
         'action'       : 'show'
        ,'what'         : 'membre'
        ,'membre_name'  : name
        ,'show_admin'   : p.show_admin
        },function(){
          if(jQuery.isFunction(p.onAjaxLoaded)){
            p.onAjaxLoaded();
            if(p.opened){
              if(p.opened.width){
                $this.dialog('option','width',p.opened.width);
              }
              if(p.opened.height){
                $this.dialog('option','height',p.opened.height);
              }
            }
          }
          if(jQuery.isFunction(p.callBack)) p.callBack('ajaxLoadEnd');
      });

      if(jQuery.isFunction(p.onOpen)) p.onOpen();
      if(jQuery.isFunction(p.callBack)) p.callBack('openEnd');
    }
    ,'close'      : function(){
      if(jQuery.isFunction(p.callBack)) p.callBack('close');
      if(jQuery.isFunction(p.onClose)) p.onClose();
      if(jQuery.isFunction(p.callBack)) p.callBack('closeEnd');
    }
    ,'resizeStop'      : function(){
      if(jQuery.isFunction(p.onClose)) p.onResizeStop();
    }
    

  });


  com.lolita.dialog.dialogIc(p_dialog);

}

if(typeof com.lolita.dialog.playerAdmin == 'undefined') com.lolita.dialog.playerAdmin = {};
com.lolita.dialog.playerAdmin = function(name){
  com.lolita.dialog.player(name, {
     "show_admin" : true
    ,"type"       : 'player_admin'
    ,"height"     : 600
  });
}


if(typeof com.lolita.get    == 'undefined') com.lolita.get    = {};
if(typeof com.lolita.get.ic == 'undefined') com.lolita.get.ic = {};
com.lolita.get.ic  = function (){
  var $ic = jQuery('#ic');

  if($ic.length==0){
    $ic = jQuery('<div></div>',{
       'id' : 'ic'
      ,'class' : ['none']
    }).prependTo('body');
  }
  return $ic;
}
